package com.example.mapp;

import com.example.entity.Elevator;
import com.example.ext.entity.ExtElevator;
import com.example.mapp.buildSql.ElevatorMappSql;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * Created by youkun on 2017/9/22.
 */
@Mapper
public interface ElevatorMapp {

    @Select( " select a.*,e.user_name as safeUserName,b.type_name as elevatorTypeName, c.party_name as usedPartyName, c.credit_code as creditCode," +
            "d.party_name as maintenancePartyName, f.check_end_dt as checkEndDt" +
            " from elevator a " +
            "left join elevator_type b on a.elevator_type_id = b.id  " +
            "left join party c on c.id = a.used_party_id  " +
            "left join party d on d.id = a.maintenance_party_id " +
            "left join daily_inspection f on f.elevator_id=a.id " +
            "left join sys_user e on a.safe_user_id = e.id "+
            " where a.id=#{id} " +
            " ORDER BY f.id desc limit 1 " )
    ExtElevator queryById(String id) throws Exception;

    @SelectProvider(type = ElevatorMappSql.class,method = "queryList")
    List<ExtElevator> queryList(ExtElevator elevator) throws Exception;

    @SelectProvider(type = ElevatorMappSql.class,method = "queryMaintList")
    List<ExtElevator> queryMaintList(Map<String, Object> elevator) throws Exception;

    @SelectProvider(type = ElevatorMappSql.class,method = "queryJgList")
    List<ExtElevator> queryJgList(Map<String, Object> elevator) throws Exception;



    @Select("select * from elevator where elevator_num=#{elevatorNum}")
    Elevator queryByElevatorNum(String elevatorNum) throws Exception;

    @Insert("insert into elevator ( angle, be_used_date, " +
            "      build_addr, build_party_name, building_info, " +
            "      create_company_name, drive_way, elevator_addr, " +
            "      elevator_area, elevator_building_num, elevator_city, " +
            "      elevator_model, elevator_num, elevator_photo_url, " +
            "      elevator_province, elevator_street, elevator_type_id, " +
            "      examination_party_id, examination_party_name, " +
            "      floor_station, governor_ck_date, insurance_date, " +
            "      insurance_name, insurance_num, is_stop, " +
            "      last_maintenance_time, lift_height, maint_tel, " +
            "      maintenance_party_id, maintenance_user_first, " +
            "      maintenance_user_second, next_check_date, next_halfmon_maint_date, " +
            "      next_halfyear_maint_date, next_quarterly_mainte_date, " +
            "      next_year_maint_date, outside_num, points_num, " +
            "      qr_code_url, rated_load, rescue_phone_num, " +
            "      run_speed, safe_user_id, safe_user_tel, " +
            "      serial_num, step_width, url, " +
            "      used_num, used_party_id,brand_Name,branch_office,place_type,elevator_place,"
            + "    examination_user_first,examination_user_second,inner_id,last_inspect_date,program_name)" +
            "    values ( #{angle}, #{beUsedDate}, " +
            "      #{buildAddr}, #{buildPartyName}, #{buildingInfo}, " +
            "      #{createCompanyName}, #{driveWay}, #{elevatorAddr}, " +
            "      #{elevatorArea}, #{elevatorBuildingNum}, #{elevatorCity}, " +
            "      #{elevatorModel}, #{elevatorNum}, #{elevatorPhotoUrl}, " +
            "      #{elevatorProvince}, #{elevatorStreet}, #{elevatorTypeId}, " +
            "      #{examinationPartyId}, #{examinationPartyName}, " +
            "      #{floorStation}, #{governorCkDate}, #{insuranceDate}, " +
            "      #{insuranceName}, #{insuranceNum}, #{isStop}, " +
            "      #{lastMaintenanceTime}, #{liftHeight}, #{maintTel}, " +
            "      #{maintenancePartyId}, #{maintenanceUserFirst}, " +
            "      #{maintenanceUserSecond}, #{nextCheckDate}, #{nextHalfmonMaintDate}, " +
            "      #{nextHalfyearMaintDate}, #{nextQuarterlyMainteDate}, " +
            "      #{nextYearMaintDate}, #{outsideNum}, #{pointsNum}, " +
            "      #{qrCodeUrl}, #{ratedLoad}, #{rescuePhoneNum}, " +
            "      #{runSpeed}, #{safeUserId}, #{safeUserTel}, " +
            "      #{serialNum}, #{stepWidth}, #{url}, " +
            "      #{usedNum}, #{usedPartyId},#{brandName},#{branchOffice},#{placeType},#{elevatorPlace},"
            + "    #{examinationUserFirst},#{examinationUserSecond},#{innerId},#{lastInspectDate},#{programName})")
    @SelectKey(statement="select last_insert_id()",keyProperty = "id",keyColumn = "id",before = false,resultType = Long.class)
    int add(Elevator elevator) throws Exception;

    @Update("update  elevator set angle=#{angle}, be_used_date=#{beUsedDate},  " +
            "      build_addr=#{buildAddr}, build_party_name=#{buildPartyName}, building_info=#{buildingInfo}, " +
            "      create_company_name=#{createCompanyName}, drive_way=#{driveWay}, elevator_addr=#{elevatorAddr}, " +
            "      elevator_area=#{elevatorArea}, elevator_building_num=#{elevatorBuildingNum}, elevator_city=#{elevatorCity}, " +
            "      elevator_model=#{elevatorModel}, elevator_num=#{elevatorNum}, elevator_photo_url=#{elevatorPhotoUrl}, " +
            "      elevator_province=#{elevatorProvince}, elevator_street=#{elevatorStreet}, elevator_type_id=#{elevatorTypeId}, " +
            "      examination_party_id=#{examinationPartyId}, examination_party_name=#{examinationPartyName}, " +
            "      floor_station=#{floorStation}, governor_ck_date =#{governorCkDate}, insurance_date=#{insuranceDate}, " +
            "      insurance_name=#{insuranceName}, insurance_num=#{insuranceNum}, is_stop=#{isStop}, " +
            "      last_maintenance_time=#{lastMaintenanceTime}, lift_height=#{liftHeight}, maint_tel=#{maintTel}, " +
            "      maintenance_party_id=#{maintenancePartyId}, maintenance_user_first=#{maintenanceUserFirst}, " +
            "      maintenance_user_second=#{maintenanceUserSecond}, next_check_date=#{nextCheckDate}, next_halfmon_maint_date=#{nextHalfmonMaintDate}, " +
            "      next_halfyear_maint_date=#{nextHalfyearMaintDate}, next_quarterly_mainte_date=#{nextQuarterlyMainteDate}, " +
            "      next_year_maint_date=#{nextYearMaintDate}, outside_num=#{outsideNum}, points_num=#{pointsNum}, " +
            "      qr_code_url=#{qrCodeUrl}, rated_load=#{ratedLoad}, rescue_phone_num=#{rescuePhoneNum}, " +
            "      run_speed=#{runSpeed}, safe_user_id=#{safeUserId}, safe_user_tel=#{safeUserTel}, " +
            "      serial_num=#{serialNum}, step_width=#{stepWidth}, url=#{url}, " +
            "      used_num=#{usedNum}, used_party_id=#{usedPartyId},brand_Name=#{brandName},"
            + "    branch_office=#{branchOffice},place_type=#{placeType}, elevator_place=#{elevatorPlace},examination_user_first=#{examinationUserFirst},examination_user_second=#{examinationUserSecond},"
            + "    inner_id=#{innerId},last_inspect_date=#{lastInspectDate},program_name=#{programName},branch_office = #{branchOffice} where id=#{id}")
    int update(Elevator elevator) throws Exception;

    @Delete("delete from elevator where id=#{id}")
    int deleteById(String id) throws Exception;

    @Update("update elevator a set last_maintenance_time=#{lastMaintenanceTime},next_halfmon_maint_date=#{nextHalfmonMaintDate} where elevator_num=#{elevatorNum}")
    int updatehmMaintDate(Map inMap) throws Exception;

    @Update("update elevator a set last_maintenance_time=#{lastMaintenanceTime},next_quarterly_mainte_date=#{nextQuarterlyMainteDate} where elevator_num=#{elevatorNum}")
    int updateqMaintDate(Map inMap) throws Exception;

    @Update("update elevator a set last_maintenance_time=#{lastMaintenanceTime},next_halfyear_maint_date=#{nextHalfyearMaintDate} where elevator_num=#{elevatorNum}")
    int updatehyMaintDate(Map inMap) throws Exception;

    @Update("update elevator a set last_maintenance_time=#{lastMaintenanceTime},next_year_maint_date=#{nextYearMaintDate} where elevator_num=#{elevatorNum}")
    int updateyMaintDate(Map inMap) throws Exception;

    @Update("update elevator set points_num=points_num+1 where id=#{id}")
    int updateLikeNum(String id) throws Exception;

    @Update("update elevator set lat=#{lat}, lng=#{lng} where id=#{id}")
    int updateLatLng(Map inmap) throws Exception;

    @Update("update elevator set place_type=#{placeType}, elevator_place=#{elevatorPlace} where id=#{id}")
    int updateElevatorPlace(Map inmap) throws Exception;

    @Select("select * from elevator where used_party_id = #{usedPartyId}")
    List<Elevator> queryByUsedPartyId(String usedPartyId) throws Exception;

    @SelectProvider(type = ElevatorMappSql.class,method = "getLatLng")
    List<Map> getLatLng(Map inmap,String mark) throws Exception;

    @SelectProvider(type = ElevatorMappSql.class,method = "updateSafeUser")
    Integer updateSafeUser(Map inMap) throws  Exception;

    @Select("select * from elevator where elevator_num = #{elevatorNum}")
    ExtElevator getElevatorInfoByNum(Map map) throws Exception;

    @Update("update elevator set mark=#{mark} where id=#{id}")
    int updateMark(Elevator elevator);

    @Update("update elevator set technique_mark=#{techniqueMark} where id=#{id}")
    int updateTechniqueMark(Elevator elevator);

    @Select("select sum(ifnull(mark,0)) as mark  from elevator where  used_party_id=#{partyId} ")
    Map getSumMark(String partyId);

    @Select("select sum(ifnull(technique_mark,0)) as techniqueMark  from elevator where  used_party_id=#{partyId} ")
    Map getSumTechniqueMark(String partyId);
    // 双随机一公开查询电梯信息
    @SelectProvider(type = ElevatorMappSql.class,method = "getElevatorList")
    List<Map<String, Object>> getElevatorList(Map<String, Object> param);

    /**
     * 按需维保查询电梯信息
     * @param elevator
     * @return
     */
    @SelectProvider(type = ElevatorMappSql.class,method = "queryElevatorList")
    List<ExtElevator> demandMaintGetElevators(ExtElevator elevator) throws Exception;

    /**
     * 按需维保查询某使用单位下所有台账的制造单位
     */
    @Select("select distinct create_company_name as createCompanyName from elevator where used_party_id=#{usedPartyId} ")
    List<String> queryCreateCompanys(ExtElevator elevator) throws Exception;

    /**
     * 按需维保更新电梯是否参与维保与维保变更天数
     * @param inMap
     * @return
     * @throws Exception
     */
    @UpdateProvider(type = ElevatorMappSql.class,method = "elevatorDemandMaintanceDay")
    int updateElevatorDemandMaintanceDay(Map inMap) throws Exception;
}
